package com.example.goodpet.utils;

import android.annotation.SuppressLint;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateUtils {
//    public String getNowDate(){
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String today = sdf.format(date);
//        return today;
//    }
    public final static double DAY_MILLIONS=86400000;

    /**
     * 从yyyy--MM--dd 字符串形式转化成Date
     * @param date
     * @return
     */
    public static Date getDate(String date){
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return  simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**]
     * 获取目前的时间
     * @return
     */
    public static Date getNowDate(){
        return new Date(System.currentTimeMillis());
    }
    /**
     * 返回date1和date2之间相差的天数,
     * @param date1
     * @param date2
     * @return
     */
    public static int getDiff(Date date1,Date date2){
        //一天86400000毫秒
        long time = date1.getTime();
        long time1 = date2.getTime();
        double ans;
        if (time>time1){
            ans = (time - time1) / DAY_MILLIONS;
        }else {
            ans = (time1 - time) / DAY_MILLIONS;
        }
        return (int)ans;
    }
}
